//
//  SecondViewController.m
//  电影豆瓣--完整版
//
//  Created by dllo on 15/12/31.
//  Copyright © 2015年 lanou. All rights reserved.
//

#import "SecondViewController.h"
#import "movieTableViewCell.h"
#import "movieModel.h"
#import "UIImageView+WebCache.h"
#import "MovieThreeCell.h"
#import "MyCollectionViewCell.h"
#import "MovieMessageViewController.h"

@interface SecondViewController ()<UITableViewDataSource, UITableViewDelegate, UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) UICollectionView *collection;
@property (nonatomic, retain) NSMutableArray *modelArr;
@property (nonatomic, retain) UIBarButtonItem *item1;
@property (nonatomic, retain) UIBarButtonItem *item2;
@property (nonatomic, retain) NSMutableArray *bigArr; /** < 九宫格的大数组*/
@property (nonatomic, retain) NSMutableArray *litterArr; /** < 九宫格的小数组*/

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"电影";
    
    // 创建item
    [self createBarButtonItem];
    [self datahandle];

    
    self.tableView = [[[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 69) style:UITableViewStylePlain] autorelease];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    // 注册重用池
    [_tableView registerClass:[movieTableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:self.tableView];
    
    
    
    // collectionView的创建，放在外面是让它只创建一次
    UICollectionViewFlowLayout *layout = [[[UICollectionViewFlowLayout alloc] init] autorelease];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.minimumLineSpacing = 30;
    layout.minimumInteritemSpacing = 15;
    layout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);
    layout.itemSize = CGSizeMake((self.view.frame.size.width - 70) / 3, 180);
    self.collection  = [[[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 89) collectionViewLayout:layout] autorelease];
    _collection.delegate = self;
    _collection.dataSource = self;
    _collection.backgroundColor = [UIColor whiteColor];
    [_collection registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@"mycell"];
    
    
}



// 点击九宫格
- (void)item1Action:(UIBarButtonItem *)item {
    self.navigationItem.rightBarButtonItem = _item2;
    [self createCollectionView];
    
}

//点击抽屉
- (void)item2Action:(UIBarButtonItem *)item {
    self.navigationItem.rightBarButtonItem = _item1;
    [self createTableView];
   
}




#pragma mark - tableView协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _modelArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        movieTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        cell.imagView.image = [UIImage imageNamed:@"www.png"];
        movieModel *model = _modelArr[indexPath.row];
        cell.titleLabel.text = model.title;
        [cell.imagView2 sd_setImageWithURL:[NSURL URLWithString:model.imag]];
        return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
     return 200;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    MovieMessageViewController *movieMessVC = [[MovieMessageViewController alloc] init];
    // 属性传值
    movieModel *model = _modelArr[indexPath.row];
    movieMessVC.model = model;
    [self.navigationController pushViewController:movieMessVC animated:YES];
}

#pragma mark - collectionView协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _modelArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"mycell" forIndexPath:indexPath];
//    cell.backgroundColor = [UIColor yellowColor];
    movieModel *model = _modelArr[indexPath.row];
    NSLog(@"哈哈哈哈哈哈哈哈哈哈哈%@", model);
    [cell.imagView sd_setImageWithURL:[NSURL URLWithString:model.imag]];
    cell.titleLabel.text = model.title;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    MovieMessageViewController *movieMessVC = [[MovieMessageViewController alloc] init];
    // 属性传值
    movieModel *model = _modelArr[indexPath.row];
    movieMessVC.model = model;
    [self.navigationController pushViewController:movieMessVC animated:YES];
}


- (void)createBarButtonItem {
    /** 创建两个item， 第一次的时候是九宫格，以后通过判断item来执行tableView或collectionView*/
    self.item1 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_nav_collection@2x.png"] style:UIBarButtonItemStylePlain target:self action:@selector(item1Action:)];
    // 先不添加
    self.item2 = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_nav_list@2x.png"] style:UIBarButtonItemStylePlain target:self action:@selector(item2Action:)] autorelease];
    self.navigationItem.rightBarButtonItem = _item1;
    [_item1 release];
    
}



- (void)createTableView {
    [self.view addSubview:self.tableView];
}



- (void)datahandle {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"movie" ofType:@"plist"];
    NSArray *tempArr = [NSArray arrayWithContentsOfFile:path];
    self.modelArr = [NSMutableArray array];
    for (NSDictionary *dic in tempArr) {
        movieModel *model = [movieModel movieModel];
        [model setValuesForKeysWithDictionary:dic];
        [_modelArr addObject:model];
    }
}



- (void)createCollectionView {
    
    [self.view addSubview:_collection];
  
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
